package com.salesys.gr.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import com.salesys.gr.util.FrmHelper;
import com.salesys.gr.util.MsgBox;
import com.salesys.tony.service.UserService;

public class FrmResetPSW {

	protected Shell FrmResetPSW;
	private Text txtUsername;
	private Text txtEmail;

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		FrmResetPSW.open();
		FrmResetPSW.layout();
		while (!FrmResetPSW.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * 
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		FrmResetPSW = new Shell(SWT.CLOSE);
		FrmResetPSW.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				if (arg0.keyCode == 13) {
					reset();
				} else if (arg0.keyCode == 27) {
					System.exit(1);
				}
			}
		});
		FrmResetPSW.setSize(270, 165);
		FrmResetPSW.setText("忘记密码");
		// 窗口居中
		FrmResetPSW.setLocation(new FrmHelper().setLocationCenter(FrmResetPSW));

		Label lblUsername = new Label(FrmResetPSW, SWT.NONE);
		lblUsername.setBounds(57, 29, 48, 17);
		lblUsername.setText("用户名：");

		Label lblEmail = new Label(FrmResetPSW, SWT.NONE);
		lblEmail.setBounds(57, 61, 48, 17);
		lblEmail.setText("邮　箱：");

		txtUsername = new Text(FrmResetPSW, SWT.BORDER);
		txtUsername.setBounds(111, 26, 100, 20);

		txtEmail = new Text(FrmResetPSW, SWT.BORDER | SWT.PASSWORD);
		txtEmail.setBounds(111, 58, 100, 20);

		Button btnConfirm = new Button(FrmResetPSW, SWT.NONE);
		btnConfirm.addMouseListener(new MouseAdapter() {
			@Override
			// 用户登录
			public void mouseUp(MouseEvent e) {
				reset();
			}
		});
		btnConfirm.setBounds(166, 97, 88, 30);
		btnConfirm.setText("申请重置");
	}

	public void reset() {
		int affect = 0;
		if (txtUsername.getText() == "") {
			new MsgBox().errorTip(FrmResetPSW, "用户名不能为空");
		} else if (txtEmail.getText() == "") {
			new MsgBox().errorTip(FrmResetPSW, "邮箱不能为空");
		} else {
			try {
				String oldEmail = new UserService().queryInfoFromUsername(txtUsername.getText()).getEmail();
				if (new UserService().queryInfoFromUsername(txtUsername.getText()).getUsername() == null) {
					new MsgBox().errorTip(FrmResetPSW, "用户名不存在，请重新填写！");
					affect = 2;
				} else if (oldEmail != null && oldEmail.equals(txtEmail.getText()) == false) {
					new MsgBox().errorTip(FrmResetPSW, "邮箱填写有误，请重新填写！");
					affect = 2;
				} else {
					affect = new UserService().resetPSW(txtUsername.getText(), txtEmail.getText());
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			if (affect == 1) {
				new MsgBox().infoTip(FrmResetPSW, "重置密码申请提交成功，请耐心等待管理员处理！");
			} else if (affect != 2) {
				new MsgBox().errorTip(FrmResetPSW, "重置密码申请提交失败，可能您已经提交申请！");
			}
		}
	}
}
